package careerSession;

import java.rmi.RemoteException;
import java.util.Collection;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;

import academicSup.AcademicSupHome;
import career.Career;
import career.CareerHome;
import city.City;
import city.CityHome;

import offer.Offer;
import offer.OfferHome;
import query.QueryHome;
import recruiter.Recruiter;
import recruiter.RecruiterHome;
import sector.Sector;
import sector.SectorHome;
import student.Student;
import student.StudentHome;
import timetable.TimetableHome;

import company.Company;
import company.CompanyHome;
import compsup.CompanySup;
import compsup.CompanySupHome;
import country.Country;
import country.CountryHome;
import cv.CVHome;
import degree.Degree;
import degree.DegreeHome;
import descriptor.CompaniesListDescriptor;
import descriptor.CompanyDescriptor;
import descriptor.CompanySupDescriptor;
import descriptor.CompanySupsListDescriptor;
import descriptor.DegreeListDescriptor;
import descriptor.FeedbackDescriptor;
import descriptor.FeedbackListDescriptor;
import descriptor.OfferDescriptor;
import descriptor.OfferListDescriptor;
import descriptor.RecruiterDescriptor;
import descriptor.RecruitersListDescriptor;
import descriptor.StudentListDescriptor;
import feedback.Feedback;
import feedback.FeedbackHome;

public class CareerSessionBean implements SessionBean
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	boolean logged = false;
	Career career = null;
	CareerHome careerHome = null;
	Company company = null;
	CompanyHome companyHome = null;
	OfferHome offerHome = null;
	Offer offer = null;
	RecruiterHome recruiterHome = null;
	Recruiter recruiter = null;
	CompanySup companySup = null;
	CompanySupHome companySupHome = null;
	City city = null;
	CityHome cityHome = null;
	Country country = null;
	CountryHome countryHome = null;
	Sector sector = null;
	SectorHome sectorHome = null;
	StudentHome studentHome = null;
	Student student = null;
	Degree degree = null;
	DegreeHome degreeHome = null;
	Feedback feedback = null;
	FeedbackHome feedbackHome = null;
	InitialContext initialContext = null;
    SessionContext ejbContext = null;
	
    public void setSessionContext(SessionContext ctx) 
    {
        ejbContext = ctx;
    }
	
    public void ejbCreate(int id) throws CreateException, RemoteException
    {
    	try 
    	{
			initialContext = new InitialContext();
			
			careerHome = (CareerHome)initialContext.lookup("MyCareer_L");
			career = careerHome.findByPrimaryKey(id);
			
			companyHome = (CompanyHome)initialContext.lookup("MyCompany_L");
			offerHome = (OfferHome)initialContext.lookup("MyOffer_L");
			recruiterHome = (RecruiterHome)initialContext.lookup("MyRecruiter_L");
			companySupHome = (CompanySupHome)initialContext.lookup("MyCompanySup_L");
			studentHome = (StudentHome)initialContext.lookup("MyStudent_L");
			degreeHome = (DegreeHome)initialContext.lookup("MyDegree_L");
			countryHome = (CountryHome)initialContext.lookup("MyCountry_L");
			cityHome = (CityHome)initialContext.lookup("MyCity_L");
			sectorHome = (SectorHome)initialContext.lookup("MySector_L");
			feedbackHome = (FeedbackHome)initialContext.lookup("MyFeedback_L");
			studentHome = (StudentHome)initialContext.lookup("MyStudent_L");
			degreeHome = (DegreeHome)initialContext.lookup("MyDegree_L");			
		} 
    	catch(Exception e){e.printStackTrace();}
    	setLogged();
    }   
	
    public boolean isLogged() throws RemoteException
    {
    	return logged;
    }
    
	public void setLogged() throws RemoteException
	{
		this.logged = true;
	}
	
	public String getSurName() throws RemoteException
	{
		return career.getSurname();
	}
	
	public String getFirstname() throws RemoteException
	{
		return career.getFirstname();
	}
	
	public String getLogin() throws RemoteException
	{
		return career.getLogin();
	}
	
	public String getMail() throws RemoteException
	{
		return career.getEmail();
	}
    
	public String getPassword() throws RemoteException
	{
		return career.getPassword();
	}
	
	public void updateMail(String email) throws RemoteException
	{
		career.setEmail(email);
	}
	
	public void updatePasswd(String passwd) throws RemoteException
	{
		career.setPassword(passwd);
	}
	
	public CompanyDescriptor getCompany(int CompID) throws RemoteException
	{
		CompanyDescriptor compDes = new CompanyDescriptor();
		Company comp = null;
		try 
		{
			comp = companyHome.findByPrimaryKey(CompID);
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		if(comp != null)
		{
			compDes = comp.getSimpleDescriptor();
		}
		return compDes;
	}
	
	public CompaniesListDescriptor getAllCompanies() throws RemoteException
	{
		CompaniesListDescriptor listComp = new CompaniesListDescriptor();
		Collection<Company> comp = null;
		try 
		{
			comp = companyHome.findAllCompanies();
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		for (Object o:comp)
		{
			listComp.addCompanyDescriptor(((Company)o).getDescriptor());
		}
		return listComp;
	}
	
	public int createCompany(String name, String address, String phone, String email, String cityName, String countryName, String sectorName) throws RemoteException
	{
		int id = -1;
		try 
		{
			company = companyHome.create(name, address, phone, email, cityName, countryName, sectorName);
			id = company.getId();
		} 
		catch (CreateException e) {e.printStackTrace();}
		return id;
	}
	
	public boolean updateCompany(int id, String name, String address, String phone, String email, String cityName, String countryName, String sectorName) throws RemoteException
	{
		boolean update = false;
		try 
		{
			company = companyHome.findByPrimaryKey(id);
			if(company != null)
			{
				company.setName(name);
				company.setAddress(address);
				company.setPhone(phone);
				company.setEmail(email);
				try 
				{
					country = countryHome.findByName(countryName);
					city = cityHome.findByName(cityName);
					sector = sectorHome.findByName(sectorName);
				} 
				catch (FinderException e) {e.printStackTrace();}
				
				if(country != null)
				{
					company.setCountry(country);
				}
				else
				{
					try 
					{
						country = countryHome.create(countryName);
					} 
					catch (CreateException e) {e.printStackTrace();}
					company.setCountry(country);
				}
				if(city != null)
				{
					company.setCity(city);
				}
				else
				{
					try 
					{
						city = cityHome.create(cityName, country);
					} 
					catch (CreateException e) {e.printStackTrace();}
					company.setCity(city);
				}
				if(sector != null)
				{
					company.setSector(sector);
				}
				else
				{
					try 
					{
						sector = sectorHome.create(sectorName);
					} 
					catch (CreateException e) {e.printStackTrace();}
					company.setSector(sector);
				}
				update = true;
			}
		} 
		catch (FinderException e) {e.printStackTrace();}
		
		return update;
	}
	
	public boolean deleteCompany(int id) throws RemoteException
	{
		boolean deleted = false;
		try 
		{
			company = companyHome.findByPrimaryKey(id);
			company.remove();
			deleted = true;
		} 
		catch (FinderException e) {e.printStackTrace();} 
		catch (RemoveException e) {e.printStackTrace();}
		return deleted;
	}
	
	public RecruiterDescriptor getRecruiter(int recruiterID) throws RemoteException
	{
		RecruiterDescriptor des = new RecruiterDescriptor();
		Recruiter recruiter = null;
		try 
		{
			recruiter = recruiterHome.findByPrimaryKey(recruiterID);
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		if(recruiter != null)
		{
			des = recruiter.getDescriptor();
		}
		return des;
	}
	
	public RecruitersListDescriptor getAllRecruiters(int compID) throws RemoteException
	{
		RecruitersListDescriptor listRec = new RecruitersListDescriptor();
		Collection<Recruiter> recruiter = null;
		try 
		{
			company = companyHome.findByPrimaryKey(compID);
			recruiter = recruiterHome.findAllRecruiter();
		} 
		catch (FinderException e){e.printStackTrace();}
		for (Recruiter r:recruiter)
		{
			if(r.getCompany().getId() == company.getId())
			{
				listRec.addRecruiterDescriptor(r.getDescriptor());
			}
		}
		return listRec;
	}
	
	public int createRecruiter(String firstName,String name, String login,String passwd,String email, int compID) throws RemoteException
	{
		int id = -1;
		try 
		{
			company = companyHome.findByPrimaryKey(compID);
			recruiter = recruiterHome.create(firstName, name, login, passwd, email, company);
			id = recruiter.getId();
		} 
		catch (FinderException e) {e.printStackTrace();} 
		catch (CreateException e) {e.printStackTrace();}
		return id;
	}
	
	public boolean updateRecruiter(int id, String firstname,String name, String login,  String email, int compID) throws RemoteException
	{
		boolean update = false;
		try 
		{
			company = companyHome.findByPrimaryKey(compID);
			recruiter = recruiterHome.findByPrimaryKey(id);
			recruiter.setFirstname(firstname);
			recruiter.setSurname(name);
			recruiter.setLogin(login);
			recruiter.setEmail(email);
			recruiter.setCompany(company);
			update = true;
		} 
		catch (FinderException e) {e.printStackTrace();} 
		return update;
	}
	
	public boolean deleteRecruiter(int id) throws RemoteException
	{
		boolean delete = false;
		try 
		{
			recruiter = recruiterHome.findByPrimaryKey(id);
			recruiter.remove();
			delete = true;
		} 
		catch (FinderException e) {e.printStackTrace();} 
		catch (RemoveException e) {e.printStackTrace();} 
		return delete;
	}
	
	public CompanySupDescriptor getSupervisor(int supervisorID) throws RemoteException
	{
		CompanySupDescriptor des = new CompanySupDescriptor();
		CompanySup companySup = null;
		try 
		{
			companySup = companySupHome.findByPrimaryKey(supervisorID);
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		if(companySup != null)
		{
			des = companySup.toCompanySupDescriptor();
		}
		return des;
	}
	
	public CompanySupsListDescriptor getAllSupervisors(int compID) throws RemoteException
	{
		CompanySupsListDescriptor listSup = new CompanySupsListDescriptor();
		Collection<CompanySup> supervisor = null;
		try 
		{
			company = companyHome.findByPrimaryKey(compID);
			supervisor = companySupHome.findAllCompanySup();
		}
		catch (FinderException e) {e.printStackTrace();}
		for (CompanySup cs:supervisor)
		{
			if(cs.getCompany().equals(company))
			{
				listSup.addCompanySupDescriptor(cs.toCompanySupDescriptor());
			}
		}
		return listSup;
	}
	
	public int createSupervisor(String firstName,String name, String login,String passwd,String email, int compID) throws RemoteException
	{
		int id = -1;
		try 
		{
			company = companyHome.findByPrimaryKey(compID);
			companySup = companySupHome.create(firstName, name, login, passwd, email, company);
			id = companySup.getId();
		} 
		catch (FinderException e) {e.printStackTrace();} 
		catch (CreateException e) {e.printStackTrace();}
		return id;
	}
	
	public boolean updateSupervisor(int id, String firstname,String name, String login,  String email, int compID) throws RemoteException
	{
		boolean update = false;
		try 
		{
			company = companyHome.findByPrimaryKey(compID);
			companySup = companySupHome.findByPrimaryKey(id);
			companySup.setFirstname(firstname);
			companySup.setSurname(name);
			companySup.setLogin(login);
			companySup.setEmail(email);
			companySup.setCompany(company);
			update = true;
		} 
		catch (FinderException e) {e.printStackTrace();} 
		return update;
	}
	
	public boolean deleteSupervisor(int id) throws RemoteException
	{
		boolean delete = false;
		try 
		{
			companySup = companySupHome.findByPrimaryKey(id);
			companySup.remove();
			delete = true;
		} 
		catch (FinderException e) {e.printStackTrace();} 
		catch (RemoveException e) {e.printStackTrace();} 
		return delete;
	}
	
	public OfferDescriptor getOffer(int offerID) throws RemoteException
	{
		OfferDescriptor des = new OfferDescriptor();
		Offer offer = null;
		try 
		{
			offer = offerHome.findByPrimaryKey(offerID);
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		if(offer != null)
		{
			des = offer.getDescriptor();
		}
		return des;
	}
	
	public OfferListDescriptor getAllOffers(int compID) throws RemoteException
	{
		OfferListDescriptor listOffer = new OfferListDescriptor();
		Collection<Offer> offer = null;
		try 
		{
			company = companyHome.findByPrimaryKey(compID);
			offer = offerHome.findAllOffers();
		} 
		catch (FinderException e) {e.printStackTrace();}
		for (Offer o:offer)
		{
			if(o.getCompany().equals(company))
			{
				listOffer.addOffer(o.getDescriptor());
			}
		}
		return listOffer;
	}
	
	public int createOffer(String name,String description, String dateOfStart, String dateOfEnd, int compID, int recruiterID, int supervisorID) throws RemoteException
	{
		int id = -1;
		try 
		{
			company = companyHome.findByPrimaryKey(compID);
			if(recruiterID != -1)
			{
				recruiter = recruiterHome.findByPrimaryKey(recruiterID);
			}
			else
			{
				recruiter = null;
			}
			if(supervisorID != -1)
			{
				companySup = companySupHome.findByPrimaryKey(supervisorID);
			}
			else
			{
				companySup = null;
			}
			offer = offerHome.create(name, description, 1, dateOfStart, dateOfEnd, company, recruiter,null,companySup);
			id = offer.getId();
		} 
		catch (CreateException e) {e.printStackTrace();} 
		catch (FinderException e) {e.printStackTrace();}
		return id;
	}
	
	public boolean updateOffer(int id,String name,String description, String dateOfStart, String dateOfEnd,  int compID, int recruiterID, int supervisorID) throws RemoteException
	{
		boolean update = false;
		try 
		{
			offer = offerHome.findByPrimaryKey(id);
			company = companyHome.findByPrimaryKey(compID);
			if(recruiterID != -1)
			{
				recruiter = recruiterHome.findByPrimaryKey(recruiterID);
			}
			else
			{
				recruiter = null;
			}
			if(supervisorID != -1)
			{
				companySup = companySupHome.findByPrimaryKey(supervisorID);
			}
			else
			{
				companySup = null;
			}
			offer.setName(name);
			offer.setDescription(description);
			offer.setType(1);
			offer.setDateOfStart(dateOfStart);
			offer.setDateOfEnd(dateOfEnd);
			offer.setCompany(company);
			offer.setRecruiter(recruiter);
			update = true;
		} 
		catch (FinderException e) {e.printStackTrace();} 
		return update;
	}
	
	public boolean deleteOffer(int id) throws RemoteException
	{
		boolean delete = false;
		try 
		{
			offer = offerHome.findByPrimaryKey(id);
			offer.remove();
			delete = true;
		} 
		catch (FinderException e) {e.printStackTrace();} 
		catch (RemoveException e) {e.printStackTrace();} 
		return delete;
	}
	
	public FeedbackDescriptor getFeedback(int feedbackID) throws RemoteException
	{
		FeedbackDescriptor des = new FeedbackDescriptor();
		Feedback feedback = null;
		try 
		{
			feedback = feedbackHome.findByPrimaryKey(feedbackID);
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		if(feedback != null)
		{
			des = feedback.getDescriptor();
		}
		return des;
	}
	
	public FeedbackListDescriptor getAllFeedbackStudent(int studenID) throws RemoteException
	{
		FeedbackListDescriptor feedbackList = new FeedbackListDescriptor();
		try
		{
			student = studentHome.findByPrimaryKey(studenID);
			if(student.getAcademicSupFeedback() != null)
				feedbackList.addFeedbackDescriptor(student.getAcademicSupFeedback().getDescriptor());
			if(student.getCompanySupFeedback() != null)
				feedbackList.addFeedbackDescriptor(student.getCompanySupFeedback().getDescriptor());
		} 
		catch (FinderException e) {e.printStackTrace();} 
		return feedbackList;	
	}
	
	public FeedbackListDescriptor getAllFeedbackDegree(int degreeID) throws RemoteException
	{
		FeedbackListDescriptor feedbackList = new FeedbackListDescriptor();
		try
		{
			degree = degreeHome.findByPrimaryKey(degreeID);
			feedbackList = degree.getFeebackDescriptors();
		} 
		catch (FinderException e) {e.printStackTrace();} 
		return feedbackList;
		
	}
	
	public StudentListDescriptor getAllStudents() throws RemoteException
	{
		StudentListDescriptor listStudent = new StudentListDescriptor();
		Collection<Student> stdt = null;
		try 
		{
			stdt = studentHome.findAllStudents();
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		for (Student s:stdt)
		{
			listStudent.addStudent(s.getDescriptor());
		}
		return listStudent;
	}
	
	public DegreeListDescriptor getAllDegrees() throws RemoteException
	{
		DegreeListDescriptor listDegree = new DegreeListDescriptor();
		Collection<Degree> deg = null;
		try 
		{
			deg = degreeHome.findAllDegrees();
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		for (Degree d:deg)
		{
			listDegree.addDegree(d.getDescriptor());
		}
		return listDegree;
	}
	
	public void ejbActivate() throws EJBException, RemoteException {}
	public void ejbPassivate() throws EJBException, RemoteException {}
	public void ejbRemove() throws EJBException, RemoteException {}
	

}
